package com.itfreer.ui.base.validations;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.itfreer.utils.common.StringUtils;

import java.util.ArrayList;

/**
 * EditText校验器
 *
 * @author ken.cai
 *
 */
public class EditTextValidator {

    private ArrayList<ValidationModel> validationModels;

    private View button;

    private Context context;

    public EditTextValidator(Context context) {
        init(context, null);
    }

    public EditTextValidator(Context context, View button) {
        init(context, button);
    }

    private void init(Context context, View button) {
        this.context = context;
        this.button = button;
        validationModels = new ArrayList<ValidationModel>();
    }

    /**
     *
     * 设置button，支持各种有点击事件的view
     *
     * @param button
     * @return
     */
    public EditTextValidator setButton(View button) {
        this.button = button;
        return this;
    }

    public EditTextValidator add(ValidationModel validationModel) {
        validationModels.add(validationModel);
        return this;
    }

    public EditTextValidator execute() {
        for (final ValidationModel validationModel : validationModels) {
            if (validationModel.getEditText() == null) {
                return this;
            }
            validationModel.getEditText().addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    setEnabled();
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        }
        setEnabled();
        return this;
    }

    private void setEnabled() {
        for (final ValidationModel validationModel : validationModels) {
            if (button != null) {
                if (validationModel.isTextEmpty()) {// 如果有一个是空的，button直接不可点击
                    button.setEnabled(false);
                    return;
                } else {
                    if (!button.isEnabled()) {
                        button.setEnabled(true);
                    }
                }
            }
        }
    }

    public String validate() {
        for (ValidationModel validationModel : validationModels) {
            // 如果没有验证处理器，直接返回正确
            if (validationModel.getValidationExecutor() == null || validationModel.getEditText() == null) {
                continue;
            }

            // 如果需要做单个EditText验证不通过标记，可以在这里实现
            String error = validationModel.getValidationExecutor().doValidate(context, validationModel.getEditText().getText().toString());
            if (!StringUtils.IsNullOrEmpty(error)) {

                // 获取焦点
                validationModel.getEditText().setFocusable(true);
                validationModel.getEditText().setFocusableInTouchMode(true);
                validationModel.getEditText().requestFocus();
                validationModel.getEditText().requestFocusFromTouch();

                error = validationModel.getValidateTitle() + "验证失败！原因：" + error;
                return error;
            }
        }
        return "";
    }

    public View getButton() {
        return button;
    }
}